//
//  IslanShowMoreCtrl.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/3/7.
//  Copyright © 2016年 game. All rights reserved.
//

#import "IslanShowMoreCtrl.h"
#import "SDPhotoGroup.h"
#import "SDPhotoBrowser.h"
#import "IslanDescribImageModel.h"
#import "Masonry.h"
#import "UIButton+WebCache.h"
@interface IslanShowMoreCtrl ()<SDPhotoBrowserDelegate>
@property (nonatomic,strong) SDPhotoBrowser *browser;
@property (nonatomic,strong) UIScrollView *bacView;

@end

@implementation IslanShowMoreCtrl
-(UIScrollView *)bacView{
    if (!_bacView) {
        _bacView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
        
        [self.view addSubview:_bacView];
    }

    return _bacView;
}
-(void)CreatNav{
    
    if (self.isImage) {
         self.Navview = (AticHeadView *)[self CreatNavViewWhithTitle:@"相册"];
    }else{
    
        self.Navview = (AticHeadView *)[self CreatNavViewWhithTitle:@"钓场简介"];
    }

    self.Navview.shareBnt.alpha = 0;
    self.Navview.collectBnt.alpha = 0;
    [self.view addSubview:self.Navview];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.app.myTabBar.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    [self CreatNav];
}


- (void)viewDidLoad {
    [super viewDidLoad];
 
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    if (self.isImage) {
        [self CreatImage];
    }else{
    
        [self Creatlab];
    }
    
}

/**
 *  用于显示简介
 */
-(void)Creatlab{

 [self.bacView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    
    UILabel *lab = [UILabel new];
    lab.font = font(SCREEN_WIDTH*0.045);
    lab.numberOfLines = 0;
    
    lab.textColor = [UIColor blackColor];
    
    lab.text = self.text;
    
    [self.bacView addSubview:lab];
    
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(@10);

        make.width.equalTo(@(SCREEN_WIDTH-20));
    }];
    
    
    CGFloat hhh = [Manager CountTextWhithSize:CGSizeMake(SCREEN_WIDTH-25, 0) AndString:self.text AndfFontSize:SCREEN_WIDTH*0.05];
    
    self.bacView.contentSize = CGSizeMake(0, hhh);
    
}


/**
 *  如果是用于显示 图片（相册）
 */
-(void)CreatImage{
    
    [self.bacView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
   
      CGFloat xd = 8;
    for (int i=0; i<self.imageAry.count; i++) {
        CGFloat ww = (SCREEN_WIDTH-xd*3)/2;

        
        NSInteger row = i%2;
        NSInteger lod = i/2;
        CGFloat xx = xd+(ww+xd)*row;
        CGFloat yy =xd+ (xd + ww) *lod;
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(xx, yy, ww, ww);
        
        IslanDescribImageModel *model = self.imageAry[i];
        
        [btn sd_setImageWithURL:[NSURL URLWithString:model.small_url] forState:UIControlStateNormal];
        
        btn.imageView.contentMode  = UIViewContentModeScaleAspectFill;
        
        btn.tag = i+10000;
        [self.bacView addSubview:btn];
        
        self.bacView.contentSize = CGSizeMake(0, Height(btn));
        
        [btn addTarget:self action:@selector(imageClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
}

-(void)imageClick:(UIButton *)btn{
    _browser = [[SDPhotoBrowser alloc] init];
    _browser.sourceImagesContainerView = self.bacView; // 原图的父控件
    _browser.imageCount = _imageAry.count; // 图片总数
    _browser.delegate = self;
    
    _browser.alpha = 0;
    _browser.currentImageIndex = (int)btn.tag-10000;
    
    [_browser show];
    
    [UIView animateWithDuration:0.8 animations:^{
        _browser.alpha = 1;
    }];

}

- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    return nil;
}

- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{
    
    IslanDescribImageModel *model = self.imageAry[index];
    
    NSString *urlStr = [model.big_url stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
    return [NSURL URLWithString:urlStr];
}

@end